import Vue from 'vue'

export default {
	install: function(Vue) {
		// 输入框点击后，选中文字
		Vue.directive('selectTextOnFocus', {
			bind: function(el, binding) {
				function bindDom(el) {
					if (el.tagName !== 'INPUT') {
						;[...el.children].forEach(dom => {
							bindDom(dom)
						})
					} else {
						el.onfocus = function() {
							setTimeout(() => {
								el.select()
							}, 30)
						}
						return true
					}
				}

				bindDom(el)
			},
		})
		// 自动聚焦
		Vue.directive('focus', {
			inserted: function(el, value) {
				value = value == 'undefined' ? true : value
				if (value) {
					el.focus()
				}
			},
		})
		
		// 防止重复提交
		Vue.directive('noMoreClick', {
			inserted(el, binding) {
				el.addEventListener('click', e => {
					let time = 5000
					if (Number(binding.arg) > 5000) {
						time = Number(binding.arg)
					}
					el.disabled = true
					setTimeout(() => {
						el.disabled = false
					}, time)
				})
			},
		})
	},
}
